package org.ax.controller.sys;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.ax.data.Result;
import org.ax.pojo.sys.User;
import org.ax.service.sys.UserService;
import org.ax.util.RedisUtil;
import org.ax.util.SendMail;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.Map;

/**
 * 编程人:赵浩鑫
 * 时间:2021-02-23-15:11
 */
@RestController
@RequestMapping("/sys")
public class CommonController {

    @Autowired
    UserService userService;
    @Autowired
    RedisUtil redisUtil;
    @Autowired
    Result result;
    @PostMapping("/login")
    public Result login(String username, String password,@RequestParam(defaultValue = "whiuhihuihidw") String vercode,@RequestParam(required = false) String rememberMe){
        vercode=vercode==""?"whiuhihuihidw":vercode;
        if(redisUtil.determineTheUser(username, 5)) {
            result.setMsg("登录次数过频繁,请你休息1分钟!!!");
            return result;
        }
        Subject subject = SecurityUtils.getSubject();
        UsernamePasswordToken token=new UsernamePasswordToken(username,password,rememberMe!=null);
//        result.setCode(2);
        try {
            subject.login(token);
            result.setMsg("ok");
//            redisUtil.del(username+"LoginNum");
//            result.setCode(1);
        }catch(UnknownAccountException ex){

            redisUtil.setLoginNumber(username, 60);
            if(redisUtil.determineTheUser(username, 5)) {
                result.setMsg("登录次数过频繁,请你休息1分钟!!!");
                return result;
            }
            result.setMsg("账号不存在,您还有"+(5-Integer.parseInt(redisUtil.get(username+"LoginNum").toString()))+"次机会");
            return result;
        }catch(IncorrectCredentialsException ex){
            if(!vercode.equals("whiuhihuihidw")){
                if(!code.equals(vercode)){
                    redisUtil.setLoginNumber(username, 60);
                    if(redisUtil.determineTheUser(username, 5)) {
                        result.setMsg("登录次数过频繁,请你休息1分钟!!!");
                        return result;
                    }
                    result.setMsg("验证码,您还有"+(5-Integer.parseInt(redisUtil.get(username+"LoginNum").toString()))+"次机会");
                    return result;
                }else{
                    User login = userService.login(username);
                    UsernamePasswordToken usernamePasswordToken2=new UsernamePasswordToken(login.getUsername(),login.getPassword(),rememberMe!=null);
                    subject.login(usernamePasswordToken2);
                    result.setMsg("ok");
                    return result;
                }
            }
            redisUtil.setLoginNumber(username, 60);
            if(redisUtil.determineTheUser(username, 5)) {
                result.setMsg("登录次数过频繁,请你休息1分钟!!!");
                return result;
            }
            result.setMsg("密码错误,您还有"+(5-Integer.parseInt(redisUtil.get(username+"LoginNum").toString()))+"次机会");
            return result;
        }

        redisUtil.del(username+"LoginNum");
        return result;

    }
    String code="wwwwwjwiodjaoidjwoijdowijdoiwjdoi";


    @GetMapping("/email")
    public Result email(String toId){
        User login = userService.login(toId);
        System.out.println("来了"+login.getEmail());
        System.out.println("toId"+login);
        code=String.valueOf(System.currentTimeMillis());
        SendMail.receiveMailAccount=login.getEmail();
        try {
            SendMail.sendEmail(code);
            result.setMsg("发送成功");
        }catch (Exception e){
            result.setMsg("发送失败");
        }
        return result;
    }
}
